﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace FixCompanyInfo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            FixIt();

            Console.ReadLine();
        }

        public static void FixIt()
        {
            const string sql = @"select distinct Companyid from CompanyTemp";

            var sqlconn = new SqlConnection
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            };

            var sqlcmd = new SqlCommand
            {
                Connection = sqlconn,
                CommandText = sql
            };

            var ds = new DataSet();
            try
            {

                var sqlda = new SqlDataAdapter(sqlcmd);

                sqlda.Fill(ds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sqlcmd = new SqlCommand();
                    sqlcmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString);
                    sqlcmd.CommandText = @"Company_FixData";
                    sqlcmd.CommandType = CommandType.StoredProcedure;

                    sqlcmd.Parameters.Add("@companyID", SqlDbType.NVarChar, 16);
                    sqlcmd.Parameters["@companyID"].Value = dr["CompanyID"].ToString();

                    try
                    {
                        sqlcmd.Connection.Open();
                        sqlcmd.ExecuteNonQuery();
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                    finally
                    {
                        sqlcmd.Connection.Close();
                    }
                }
            }
        }
    }
}
